package com.westvalley.common.result;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.westvalley.common.kit.ListKit;
import com.westvalley.common.kit.SqlsKit;


public class SqlResult {
	Map<String,List<List<Object>>> sqlInfo;
	
	private SqlResult(){
		sqlInfo = new LinkedHashMap<String, List<List<Object>>>();
	}
	
	public static SqlResult me(){
		return new SqlResult();
	}
	
	public void set(SqlsKit kit){
		if(kit!=null){
			set(kit.getSql(),kit.getSqlValues());
		}
	}
	
	public void set(String sql,List<Object> data){
		if(StrKit.notBlank(sql) && data!=null){
			List<List<Object>> dataList = sqlInfo.get(sql);
			if(dataList == null)
				dataList = new ArrayList<List<Object>>();
			dataList.add(data);
			sqlInfo.put(sql, dataList);
		}
	}
	
	public int[] batch(int batchSize){
		int[] batchResult = null;
		for(String sql : sqlInfo.keySet()){
			List<List<Object>> dataList = sqlInfo.get(sql);
			if(dataList!=null){
				batchResult = Db.batch(sql, ListKit.list2Array(dataList), batchSize);
			}
		}
		return batchResult;
	}
}
